package com.fifth.user.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fifth.common.pojo.dto.CommonResult;
import com.fifth.common.pojo.entity.UserInfo;
import com.fifth.user.service.UserService;
import com.fifth.user.util.OSSUtil;
import com.fifth.user.util.Sample;
import com.fifth.user.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @作者：刘壬杉
 * @创建时间 2022/8/31 11:30
 **/
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    /**
     * 为短信发送方法写一个接口
     * @param telephone
     * @return
     */
    @GetMapping("noteByPhone/{telephone}")
    public CommonResult noteByPhone(@PathVariable String telephone)throws Exception{
        String note = Sample.sendCode(telephone);
        return new CommonResult(2000,"发送验证码成功",note);
    }
    //注册
    @PostMapping("signInsert/{telephone}/{password}")
    public CommonResult signInsert(@PathVariable String telephone,@PathVariable String password){
        UserInfo user = userService.findByPhone(telephone);
        if (user!=null){
            return new CommonResult(4000,"改手机号已注册,请直接登录",user);
        }
        return userService.signInsert(telephone,password);
    }
    //用户登录修改个人信息
    @PostMapping("updateMessage")
    public CommonResult updateMessage(@RequestBody UserInfo userInfo){
        return userService.updateMessage(userInfo);
    }
    //修改密码
    @PostMapping("/updatePassword/{telephone}/{password}")
    public CommonResult updatePassword(@PathVariable String telephone,@PathVariable String password){
       return userService.updatePassword(telephone,password);
    }
    //查询登录用户信息
    @GetMapping("findUserById")
    public CommonResult findUserById(){
        return userService.findUserById();
    }
    //上传图片
    @PostMapping("/upload01")
    public CommonResult upload01(MultipartFile file){
        try{
            String upload = OSSUtil.upload(file);
            return new CommonResult(2000,"上传成功",upload);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new CommonResult(5000,"上传失败",null);

    }
    //根据id修改用户状态添加操作人 冻结操作
    @PostMapping("updateByid")
    public CommonResult updateByid(String id,Integer status){
        return userService.updateByid(id,status);
    }
    //后台管理查询所有信息
    @PostMapping("getUserAll")
    public CommonResult getUserAll(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "5") Integer pageSize,String startTime,String endTime){
        return userService.getUserAll(currentPage,pageSize,startTime,endTime);
    }
    //统计用户个数
    @GetMapping("numberUser")
    public CommonResult numberUser(){
        return userService.numberUser();
    }
    //统计所有店铺商品类别个数
    @GetMapping("businessTotal")
    public CommonResult businessTotal(){
        return userService.businessTotal();
    }
    @GetMapping("priceBusiness")
    public CommonResult priceBusiness(){
        return userService.priceBusiness();
    }

    @PostMapping("getShenfen")
    public CommonResult getShenfen(){
        //获取到requset
        HttpServletRequest request = WebUtils.getRequest();
        //获取token
        String token = request.getHeader("token");
        if (token==null){
            return new CommonResult(5000,"未登录",null);
        }
        //根据token获取userid和手机号
        Map userMap = JSON.parseObject(redisTemplate.opsForValue().get(token), Map.class);
        String userid;
        if (userMap != null && userMap.containsKey("user")) {
            JSONObject user = (JSONObject) userMap.get("user");
            userid = user.getString("id");
            String loginType = (String) userMap.get("loginType");
            return new CommonResult(2000,"获取身份",loginType);
        } else {
            return new CommonResult(4000, "暂未登录,请登录后再购买", null);
        }
    }
}
